package nccloud.bs.ftm.framect.headafter.rule;

import nc.bs.framework.common.NCLocator;
import nc.itf.scmpub.reference.uap.org.OrgUnitPubService;
import nc.itf.bd.cust.sale.ICustSaleQueryService;
import nc.vo.bd.cust.saleinfo.CustsaleVO;
import nc.vo.pub.BusinessException;
import nc.vo.ftm.framect.AggFramectHVO;
import nc.vo.ftm.framect.FramectHVO;


public class FramectEditRule extends AbstractHeadEditRule {
	
	@Override
	public AggFramectHVO afterEdit(AggFramectHVO billvo) throws BusinessException {
		FramectHVO hvo = billvo.getParentVO();
		String pk_customer = hvo.getPk_customer();
		if(pk_customer == null) {
			return billvo;
		}
//		ICustFinanceQueryService custFinanceQueryService = NCLocator.getInstance().lookup(ICustFinanceQueryService.class);
//		CustFinanceVO[] custFinanceVOs = custFinanceQueryService.queryCustFinanceVOsByOrgIDs(new String[] {billvo.getParentVO().getPk_org()}, pk_customer);
//		if(custFinanceVOs != null && custFinanceVOs.length > 0) {
//			CustFinanceVO custFinanceVO = custFinanceVOs[0];
//			billvo.getParentVO().setCdeptid(custFinanceVO.getPk_respdept1());
//			billvo.getParentVO().setSalesman(custFinanceVO.getPk_resppsn1());
//		}
		ICustSaleQueryService custSaleQueryService = NCLocator.getInstance().lookup(ICustSaleQueryService.class);
		CustsaleVO[] custSaleVOs = custSaleQueryService.qeuryCustSaleVOsByOrgIDs(new String[] {billvo.getParentVO().getPk_org()}, pk_customer);
		if(custSaleVOs != null && custSaleVOs.length > 0) {
			CustsaleVO custSaleVO = custSaleVOs[0];
			if(custSaleVO.getRespdept() != null) {
				billvo.getParentVO().setDepid(custSaleVO.getRespdept());
				String cdeptvid = OrgUnitPubService.getNewVIDByOrgID(custSaleVO.getRespdept());
				billvo.getParentVO().setDepid_v(cdeptvid);
			} else {
				billvo.getParentVO().setDepid_v(null);
				billvo.getParentVO().setDepid_v(null);
			}
			if(custSaleVO.getRespperson() != null) {
				billvo.getParentVO().setPersonnelid(custSaleVO.getRespperson());
			} else {
				billvo.getParentVO().setPersonnelid(null);
			}		
//			if(custSaleVO.getPaytermdefault() != null && hvo.getAgreement() == null) {
//				hvo.setAgreement(custSaleVO.getPaytermdefault());
//				new PaytermEditRule().afterEdit(billvo);
//			}
//			hvo.setTrademode(custSaleVO.getPk_tradeterm());
//			if(custSaleVO.getCurrencydefault() != null && hvo.getCurrency() == null) {
//				hvo.setCurrency(custSaleVO.getCurrencydefault());
////				new CurrencyEditRule().afterEdit(billvo);
//			}
		}
		new CurrencyEditRule().afterEdit(billvo);
		return billvo;
	}
}
